package com.thkj.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.thkj.entity.TblNotice;
import com.thkj.service.TblNoticeService;
import com.thkj.util.PushUtils;
import com.thkj.util.RequestUtil;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import dp.R;
import dp.dao.PagingList;
import dp.util.DateUtil;
import com.thkj.framework.base.BaseController;

@RestController
@RequestMapping("/backstage/notice")
public class NoticeAction extends BaseController {
    @Autowired
    TblNoticeService tblNoticeService;
    
    @RequestMapping("")
    public ModelAndView index(){
    	return new ModelAndView("notice/list");
    }
	
	@RequestMapping("/query")
    public R query(HttpServletRequest request, int page, int size){
    	Map<String, Object> param = RequestUtil.getParameterMap(request);
    	PagingList<Map> list = tblNoticeService.query(page, size,param);
    	return R.success(list);
    }
	
    @RequestMapping("/edit")
    public R edit(TblNotice notice){
    	try {
    		if(StrUtil.isBlank(notice.getId())) {
    			notice.setId(IdUtil.objectId());
    			notice.setTime(DateUtil.getNowAsTimestamp());
    			tblNoticeService.insertTblNotice(notice);
        	}else {
        		tblNoticeService.updateTblNotice(notice);
        	}
        	return R.SUCCESS;
    	}catch (Exception e) {
			return R.failure(e.getMessage());
		}
    }
    
    @RequestMapping("/delete")
    public R delete(String id){
    	tblNoticeService.deleteTblNotice(id);
    	return R.SUCCESS;
    }
    
    @RequestMapping("/push")
    public R push(String id){
    	TblNotice notice = tblNoticeService.getTblNoticeById(id);
    	if(notice!=null) {
    		PushUtils.sendForJgAll("jy", notice);
    		PushUtils.sendForJgAll("jg", notice);
    		return R.SUCCESS;
    	}else {
    		return R.failure("通知不存在！");
    	}
    }
}